import {createRouter, createWebHistory} from 'vue-router'
import {useAuthStore} from '@/stores/auth'

export const dashboard = 'Dashboard';
export const batteryPassportList = 'BatteryPassportList';
export const batteryPassportCreate = 'BatteryPassportCreate';
export const batteryPassportDetail = 'BatteryPassportDetail';
export const batteryPassportEdit = 'BatteryPassportEdit';
export const portableBatteryList = 'portableBatteryList';
export const portableBatteryCreate = 'portableBatteryCreate';
export const portableBatteryDetail = 'portableBatteryDetail';
export const portableBatteryEdit = 'portableBatteryEdit';
export const login = 'Login';
export const resetPassword = 'ResetPassword';
export const phone = 'Phone';
export const Profile = 'Profile';
export const notFound = 'NotFound';

const routes = [
    {
        path: '/',
        redirect: '/dashboard',
        component: () => import('@/views/MainLayout.vue'),
        meta: {requiresAuth: true},
        children: [
            {
                path: '/dashboard',
                name: dashboard,
                component: () => import('@/views/dashboard/Dashboard.vue'),
                meta: {title: '仪表盘'}
            },
            {
                path: '/battery-passport',
                name: batteryPassportList,
                component: () => import('@/views/BatteryPassport/List.vue'),
                meta: {title: '电池护照列表'}
            },
            {
                path: '/battery-passport/create',
                name: batteryPassportCreate,
                component: () => import('@/views/BatteryPassport/Edit.vue'),
                meta: {title: '创建电池护照'}
            },
            {
                path: '/battery-passport/detail/:id',
                name: batteryPassportDetail,
                component: () => import('@/views/BatteryPassport/Detail.vue'),
                meta: {title: '电池护照详情'}
            },
            {
                path: '/battery-passport/edit/:id',
                name: batteryPassportEdit,
                component: () => import('@/views/BatteryPassport/Edit.vue'),
                meta: {title: '编辑电池护照'}
            },
            {
                path: '/portableBattery/list',
                name: portableBatteryList,
                component: () => import('@/views/PortableBattery/List.vue'),
                meta: {title: '电池护照列表'}
            },
            {
                path: '/portableBattery/create',
                name: portableBatteryCreate,
                component: () => import('@/views/PortableBattery/Edit.vue'),
                meta: {title: '创建电池护照'}
            },
            {
                path: '/portableBattery/detail/:id',
                name: portableBatteryDetail,
                component: () => import('@/views/PortableBattery/Detail.vue'),
                meta: {title: '电池护照详情'}
            },
            {
                path: '/portableBattery/edit/:id',
                name: portableBatteryEdit,
                component: () => import('@/views/PortableBattery/Edit.vue'),
                meta: {title: '编辑电池护照'}
            },
            {
                path: '/user/profile',
                name: Profile,
                component: () => import('@/views/user/Profile.vue'),
                meta: {title: '配置'}
            },
        ]
    },
    {
        path: '/login',
        name: login,
        component: () => import('@/views/auth/Login.vue'),
        meta: {requiresAuth: false},
    },
    {
        path: '/ResetPassword',
        name: resetPassword,
        component: () => import('@/views/auth/ResetPassword.vue'),
        meta: {requiresAuth: false},
    },
    {
        path: '/public/phone/:id',
        name: phone,
        component: () => import('@/views/public/Phone.vue'),
        meta: {requiresAuth: false},
    },
    {
        path: '/:pathMatch(.*)*',
        name: notFound,
        component: () => import('@/views/404.vue')
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
})

router.beforeEach(async (to, from, next) => {
    const authStore = useAuthStore()

    // 检查是否需要认证
    if (to.meta.requiresAuth && !authStore.isAuthenticated) {
        next('/login')
        return
    }

    // 已登录用户不能访问guestOnly页面
    if (to.meta.guestOnly && authStore.isAuthenticated) {
        next('/dashboard')
        return
    }

    next()
})

export default router